package handler

import (
	"api/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "请求参数错误",
		})
		return
	}

	if file.Size > 1024*1024*10024 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "文件大小不能大于1GB ",
		})
		return
	}

	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "文件格式需为MP4 ",
		})
		return
	}

	upload, err := pkg.Upload(file)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "上传失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": upload,
	})
}
